package com.baseLesson._01_jdbc;

import java.sql.*;

public class DBUtilNew {

      final String URL1 ="22";

    //连接数据库的url
     static final String URL =
            "jdbc:mysql://192.168.0.191:3306/Test?useUnicode=true&amp;characterEncoding=utf-8";
    //登录名
    private static final String USER = "root";
    //登录密码
    private static final String PASSWORD = "root";
    //连接类：执行SQL语句，返回结果
    private static Connection conn = null;



    static {
        try {
            //1.加载驱动程序，forName里边的内容是固定的
            //--用到到【反射】的知识点
            //com.mysql.jdbc.Driver 包名+类名
            Class.forName("com.mysql.jdbc.Driver");
            //2.获得数据库连接，连接上数据库
            conn = DriverManager.getConnection(URL, USER, PASSWORD);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


    public static void main(String[] args) throws SQLException {
        System.out.println(conn);

        select("1");
        select("2");
    }


    public static void select(Biology biology) throws SQLException {

        String sql = "select * from Biology where id = ? ";
        //创建PreparedStatement对象，用来向数据库发送带参数的sql语句
        PreparedStatement pstm = conn.prepareStatement(sql);
        //按照顺序设置参数
        pstm.setString(1, biology.getId());

        //执行SQL语句，返回查询的结果
        ResultSet rs = pstm.executeQuery();
        //判断是否有下一行数据
        while (rs.next()) {
            System.out.println(rs.getString("name"));
            System.out.println(rs.getInt("needCount"));
        }

    }
    public static void select(String id) throws SQLException {

        String sql = "select * from Biology where id = ?";
        //创建PreparedStatement对象，用来向数据库发送带参数的sql语句
        PreparedStatement pstm = conn.prepareStatement(sql);
        //按照顺序设置参数
        pstm.setString(1, id);

        //执行SQL语句，返回查询的结果
        ResultSet rs = pstm.executeQuery();
        //判断是否有下一行数据
        while (rs.next()) {
            System.out.println(rs.getString("name"));
            System.out.println(rs.getInt("needCount"));
        }

    }

}
